<template>
  <div class="app-container" v-enter-helper="getList">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-form
            :inline="true"
            :model="listQuery"
            ref="listQuery"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-select
                filterable
                v-model="listQuery.sex"
                placeholder="请选择性别"
                clearable
              >
                <el-option
                  v-for="item in sexOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-select
                filterable
                v-model="listQuery.app_status"
                placeholder="请选择状态"
                clearable
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.nickname"
                placeholder="请输入昵称"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.user_code"
                placeholder="请输入用户ID"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item" v-loading="listLoading">
        <el-table
          ref="dataList"
          :data="menuList"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="user_code" label="用户ID"> </el-table-column>
          <el-table-column prop="nickname" label="昵称"> </el-table-column>
          <el-table-column prop="full_name" label="姓名"> </el-table-column>
          <el-table-column prop="id_number" label="身份证号"> </el-table-column>
          <el-table-column prop="sex" label="性别">
            <template slot-scope="scope">
              <p v-if="scope.row.sex === 1">男</p>
              <p v-if="scope.row.sex === 2">女</p>
            </template>
          </el-table-column>
          <el-table-column prop="id_card" label="审核照片" width="340px">
            <template slot-scope="scope">
              <el-popover
                placement="left"
                max-width="420"
                trigger="hover"
                style="margin-right:10px"
              >
                <img
                  :src="scope.row.id_card"
                  alt=""
                  style="max-width:300px;max-height:500px"
                />
                <img
                  slot="reference"
                  :src="scope.row.id_card"
                  alt=""
                  style="width: 100px;"
                />
              </el-popover>
              <el-popover
                placement="left"
                max-width="420"
                trigger="hover"
                style="margin-right:10px"
              >
                <img
                  :src="scope.row.front"
                  alt=""
                  style="max-width:300px;max-height:500px"
                />
                <img
                  slot="reference"
                  :src="scope.row.front"
                  alt=""
                  style="width: 100px;"
                />
              </el-popover>
              <el-popover placement="left" max-width="420" trigger="hover">
                <img
                  :src="scope.row.back"
                  alt=""
                  style="max-width:300px;max-height:500px"
                />
                <img
                  slot="reference"
                  :src="scope.row.back"
                  alt=""
                  style="width: 100px;"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="creatde_time" label="提交时间">
          </el-table-column>
          <el-table-column prop="app_status" label="状态">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.app_status === 1"
                >审核通过</el-tag
              >
              <el-tag type="danger" v-if="scope.row.app_status === 2"
                >审核不通过</el-tag
              >
              <el-tag type="primary" v-if="scope.row.app_status === 0"
                >审核中</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="reason" label="不通过理由"> </el-table-column>

          <el-table-column label="操作" fixed="right" width="250">
            <template slot-scope="scope">
              <!-- <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)" plain>编辑</el-button>
                  <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)" plain>删除</el-button> -->
              <el-button
                v-if="scope.row.app_status === 0"
                type="primary"
                @click="handleCheck(scope.row)"
                size="mini"
                icon="el-icon-check"
                plain
                >审核</el-button
              >
              <el-button
                v-else
                type="danger"
                @click="handleRecheck(scope.row)"
                size="mini"
                icon="el-icon-refresh"
                plain
                >撤销审核</el-button
              >
              <el-button
                v-if="scope.row.deleted === 0"
                plain
                size="mini"
                type="danger"
                @click="handleBlacklist(scope.row)"
              >加入黑名单
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog title="信息" :visible.sync="deleteDialogVisible" width="435px">
      <span style="">确定执行此操作？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="审核"
      :visible.sync="checkDialogVisible"
      @close="handleDialogClose('editRulesForm')"
    >
      <el-form
        :model="editRulesForm"
        ref="editRulesForm"
        :rules="editRulesFormRules"
        label-width="120px"
      >
        <el-form-item label="审核结果" prop="app_status">
          <el-radio v-model="editRulesForm.app_status" :label="1"
            >通过</el-radio
          >
          <el-radio v-model="editRulesForm.app_status" :label="2"
            >拒绝</el-radio
          >
        </el-form-item>
        <el-form-item label="审核理由" prop="reason">
          <el-input
            v-model="editRulesForm.reason"
            placeholder="请输入审核理由"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="checkDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmCheck">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listData, postData, deleteData } from "@/api/member/realName";
import { postBlack } from "@/api/member/blackList";
export default {
  data() {
    return {
      confirmBtn: false,

      statusOptions: [
        { value: 0, label: "审核中" },
        { value: 1, label: "审核通过" },
        { value: 2, label: "不通过" }
      ],
      sexOptions: [
        { value: 1, label: "男" },
        { value: 2, label: "女" }
      ],
      // 要修改的当前行的id
      row_id: 0,
      // 表单规则
      editRulesFormRules: {
        reason: [
          { required: true, message: "请输入审核理由", trigger: "blur" }
        ],
        app_status: [
          { required: true, message: "请选择审核结果", trigger: "change" }
        ]
      },
      editRulesForm: {
        reason: undefined,
        app_status: 1
      },
      checkDialogVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10,
        app_status: undefined,
        sex: undefined,
        user_code: undefined,
        nickname: undefined
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      let urlData = "?";
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`;
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`;
      }
      if (this.listQuery.user_code === 0 || this.listQuery.user_code) {
        urlData = urlData + `&user_code=${this.listQuery.user_code}`;
      }
      if (this.listQuery.nickname === 0 || this.listQuery.nickname) {
        urlData = urlData + `&nickname=${this.listQuery.nickname}`;
      }
      if (
        this.listQuery.app_status === 0 ||
        this.listQuery.app_status === 1 ||
        this.listQuery.app_status === 2
      ) {
        urlData = urlData + `&app_status=${this.listQuery.app_status}`;
      }
      if (this.listQuery.sex) {
        urlData = urlData + `&sex=${this.listQuery.sex}`;
      }
      listData(urlData).then(response => {
        // response)
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false;
        this.confirmBtn = false;
      });
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10;
      this.listQuery.sex = undefined;
      this.listQuery.app_status = undefined;
      this.listQuery.user_code = undefined;
      this.listQuery.nickname = undefined;
      // this.getList()
    },
    handleCheck(row) {
      this.editRulesForm = {
        reason: undefined,
        app_status: 1
      };
      this.row_id = row.id;
      this.checkDialogVisible = true;
    },
    confirmCheck() {
      if (this.confirmBtn) return;
      this.$refs["editRulesForm"].validate(valid => {
        if (valid) {
          this.confirmBtn = true;
          postData(this.row_id, this.editRulesForm).then(response => {
            if (response.code === 200) {
              this.getList();
              this.confirmBtn = false;
              this.checkDialogVisible = false;
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
            } else {
              this.confirmBtn = false;
            }
          });
        } else {
          // 'error submit!!');
          return false;
        }
      });
    },
    handleRecheck(row) {
      // row)
      this.row_id = row.id;
      this.deleteDialogVisible = true;
    },
    confirmDelete() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;
      deleteData(this.row_id).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.confirmBtn = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
      }
    },
    PostBlack(data) {
      postBlack(data).then(res => {
        if(res.code === 200) {
          this.getList();
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        }
      })
    },
    handleBlacklist(row) {
      this.$confirm('是否加入黑名单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.PostBlack({
          name: row.full_name,
          id_number: row.id_number
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    }
  }
};
</script>

<style scoped>
[v-cloak] {
  display: none;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
